package com.earnings.okhttputils.utils.ui.fragment.transaction;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.earnings.R;
import com.earnings.okhttputils.utils.ui.BaseFragment;
import com.earnings.okhttputils.utils.ui.activity.user.UserBalanceRechargeActivity;

import java.io.File;

import cn.finalteam.rxgalleryfinal.RxGalleryFinal;
import cn.finalteam.rxgalleryfinal.RxGalleryFinalApi;
import cn.finalteam.rxgalleryfinal.imageloader.ImageLoaderType;
import cn.finalteam.rxgalleryfinal.rxbus.RxBusResultSubscriber;
import cn.finalteam.rxgalleryfinal.rxbus.event.ImageRadioResultEvent;
import cn.finalteam.rxgalleryfinal.ui.base.IRadioImageCheckedListener;


/**
 * 线下充值-步骤1
 * Created by HOHO on 2017/4/11.
 */

public class RechargeOfflineStepFirstFragment extends BaseFragment {

    private ImageView image;


    @Override
    protected void init() {
        mView.findViewById( R.id.sumbit).setOnClickListener(this);
        image= (ImageView) mView.findViewById( R.id.image );
        image.setOnClickListener( this );
        ((TextView)mView.findViewById( R.id.accountName)).setText( ((UserBalanceRechargeActivity)getActivity()).rechargeData.getName() );
        ((TextView)mView.findViewById( R.id.accountBanks)).setText( ((UserBalanceRechargeActivity)getActivity()).rechargeData.getBank() );
        ((TextView)mView.findViewById( R.id.accountBanknum)).setText( ((UserBalanceRechargeActivity)getActivity()).rechargeData.getAccount() );

      copy( (TextView)mView.findViewById( R.id.accountName) ,mView.findViewById( R.id.copy1));
        copy( (TextView)mView.findViewById( R.id.accountBanks) ,mView.findViewById( R.id.copy2));
        copy( (TextView)mView.findViewById( R.id.accountBanknum) ,mView.findViewById( R.id.copy3));
    }

    @Override
    protected int onCreateLayout() {
        return R.layout.fragment_recharge_offline_step1;
    }




    @Override
    public void onClick(View v) {

        int i = v.getId();
        if (i == R.id.sumbit) {
            if(((RechargeOfflineFragment) getActivity().getSupportFragmentManager().getFragments().get( 1 )).imgFile!=null)
            ((RechargeOfflineFragment) getActivity().getSupportFragmentManager().getFragments().get( 1 )).runStep( RechargeOfflineFragment.STEP_SCONED );
            else
                showToast( "请上传您的银行汇款凭证" );
        }else if (i == R.id.image) {
            openPictrue();

        }
    }


    private static final int PERMISSIONS_REQUEST_CALL_CAMERA = 1;
    public void openPictrue() {
        if (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.CAMERA ) != PackageManager.PERMISSION_GRANTED) {
            //如果没有授权，则请求授权
            ActivityCompat.requestPermissions( getActivity(), new String[]{android.Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CALL_CAMERA );
        } else {

            RxGalleryFinalApi.onCropImageResult( new IRadioImageCheckedListener() {
                @Override
                public void cropAfter(Object t) {
                    ((RechargeOfflineFragment) getActivity().getSupportFragmentManager().getFragments().get( 1 )).imgFile= (File) t;
                    Glide.with(getActivity()).load((File)t).into(image);

                }

                @Override
                public boolean isActivityFinish() {
                    return true;
                }
            } );
            RxGalleryFinal
                    .with(getContext())
                    .image()
                    .radio()
                    .crop()
                    .imageLoader( ImageLoaderType.GLIDE )
                    .subscribe( new RxBusResultSubscriber<ImageRadioResultEvent>() {
                        @Override
                        protected void onEvent(ImageRadioResultEvent baseResultEvent) throws Exception {

                        }
                    } )
                    .openGallery();


        }
    }

    public  void copy(final TextView str, View onclick) {
        onclick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 得到剪贴板管理器
                ClipboardManager cmb = (ClipboardManager) getActivity()
                        .getSystemService( Context.CLIPBOARD_SERVICE);
                cmb.setText(str.getText().toString().trim());
                Toast.makeText( getActivity(),"已复制到剪贴板",Toast.LENGTH_SHORT).show();
            }
        });

    }
}
